/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package bombercloud;

import java.awt.Color;
import java.util.Random;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;

/**
 *
 * @author Leonardo Oliveira Moreira
 */
public class Player extends Thread {

    private long id;
    private int x;
    private int y;

    private JPanel avatar;

    private Scenario scenario;

    private int avatarHeight;
    private int avatarWidth;

    public Player(Scenario scenario) {
        this.scenario = scenario;
        this.id = System.nanoTime();
        this.avatar = new JPanel(null);
        this.x = 0;
        this.y = 0;
        this.avatarHeight = scenario.getSizeOfAvatar().height;
        this.avatarWidth = scenario.getSizeOfAvatar().width;
        this.avatar.setSize(avatarWidth, avatarHeight);
        JLabel icon = new JLabel(new ImageIcon(Player.class.getResource("bomberman.gif")));
        icon.setSize(this.avatar.getSize());
        this.avatar.add(icon);
    }

    public void up() {
        if (y == 0) {
            return;
        }
        if (scenario.isCollision(this, x, y - avatarHeight)) {
            return;
        }
        y = y - avatarHeight;
        avatar.setLocation(getX(), getY());
    }

    public void down() {
        if (y == scenario.getScenarioHeight() - avatarHeight) {
            return;
        }
        if (scenario.isCollision(this, x, y + avatarHeight)) {
            return;
        }
        y = y + avatarHeight;
        avatar.setLocation(getX(), getY());
    }

    public void left() {
        if (x == 0) {
            return;
        }
        if (scenario.isCollision(this, x - avatarWidth, y)) {
            return;
        }
        x = x - avatarWidth;
        avatar.setLocation(getX(), getY());
    }

    public void right() {
        if (x == scenario.getScenarioWidth() - avatarWidth) {
            return;
        }
        if (scenario.isCollision(this, x + avatarWidth, y)) {
            return;
        }
        x = x + avatarWidth;
        avatar.setLocation(getX(), getY());
    }

    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }

    public void setX(int x) {
        this.x = x;
    }

    public void setY(int y) {
        this.y = y;
    }
    
    @Override
    public void run() {
        Random random = new Random();
        while (true) {
            int action = random.nextInt(4);
            switch (action) {
                case 0: {
                    up();
                    break;
                }
                case 1: {
                    down();
                    break;
                }
                case 2: {
                    left();
                    break;
                }
                case 3: {
                    right();
                    break;
                }
            }
            scenario.updatePlayerPosition(this);
            try {
                sleep(100);
            } catch (InterruptedException ex) {
            }
        }
    }

    public JPanel getAvatar() {
        return avatar;
    }
    
    public int getAvatarWidth() {
        return avatarWidth;
    }
    
    public int getAvatarHeight() {
        return avatarHeight;
    }

}
